#include "show_comment.h"
#include "ui_show_comment.h"
#include<QMessageBox>
#include <QString>
#include <QtSql>
#include <QSqlDatabase>
#include "dialog.h"
#include <QSqlQuery>
#include <QDebug>
show_comment::show_comment(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::show_comment)
{
    ui->setupUi(this);
}

show_comment::~show_comment()
{
    delete ui;
}

void show_comment::on_pushButton_clicked()
{
    QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");//添加驱动
     data_base.setHostName("47.110.157.197");  //设置主机地址
     data_base.setPort(3306);  //mysql设置端口
     data_base.setDatabaseName("myproject");  //设置数据库名称
     data_base.setUserName("root");  //设置用户名
     data_base.setPassword("420683ABc@");  //设置密码
     if(!data_base.open())//打开数据库
     {
         qDebug()<<"connect failed";
         qDebug() << data_base.lastError();//.databaseText()  输出错误信息
     }
     else
         qDebug()<<"success";
    QSqlQuery query(data_base);
    query.exec("select usr_name,comments from usr_comment");
    int i=0;
    QString comment="";
    while(query.next()&&i<20){
        i++;
        QString username=query.value(0).toString();
        QString user_comment =query.value(1).toString();
        comment=comment+username+"  "+user_comment+"\n";
    }
   ui->textBrowser->setText(comment);
}

void show_comment::on_pushButton_2_clicked()
{
    this->close();
}
